package com.tuyue.police.biz.impl;

import com.tuyue.police.bean.RuleBean;
import com.tuyue.police.biz.IRuleBiz;
import com.tuyue.police.dao.*;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/3/15.
 * @Modified By:
 */
@Service
public class RuleBizImpl implements IRuleBiz {

    @Autowired
    private IRuleDao ruleDao;
    @Autowired
    private IRule1Dao rule1Dao;
    @Autowired
    private IRule2Dao rule2Dao;
    @Autowired
    private IDispenseWayDao dispenseWayDao;
    @Autowired
    private IUserDao userDao;
    @Autowired
    private ISectorDao sectorDao;
    @Override
    public Page<Rule> findAllByUserId(Long userId, Pageable pageable) {
        Page<Rule> ruleList = ruleDao.findAllByRuleUserId(userId,pageable);
//        Page<Rule> ruleList = ruleDao.findAll(new Specification<Rule>() {
//            @Override
//            public Predicate toPredicate(Root<Rule> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                return null;
//            }
//        },pageable);
        ruleList.forEach(rule -> {
            DispenseWay dispenseWay = dispenseWayDao.findOne(rule.getRuleDispenseId());
            if (!ObjectUtils.isEmpty(dispenseWay)) {
                rule.setRuleDispenseName(dispenseWay.getDispenseName());
            }
        });
        return ruleList;
    }

    @Override
    public RuleBean findById(Long rId) {
        Rule rule = ruleDao.findOne(rId);
        if (ObjectUtils.isEmpty(rule)) {
            throw new MyException(ResultEnum.RULE_ERROR);
        }
        RuleBean ruleBean = new RuleBean();
        BeanUtils.copyProperties(rule,ruleBean);
        List<Rule1> rule1List = rule1Dao.findAllByRule1RuleId(rule.getRuleId());
        ruleBean.setRule1s(rule1List);
        List<Rule2> rule2s=new ArrayList<>();
        List<Rule2> rule2List = rule2Dao.findAllByRule2RuleId(rule.getRuleId());
        for (Rule2 rule2 : rule2List) {
            rule2.setRule2UserTel((userDao.getOne(rule2.getRule2UserId())).getUserName());
            rule2s.add(rule2);
        }
        ruleBean.setRule2s(rule2s);
        return ruleBean;
    }

    @Transactional
    @Override
    public RuleBean add(RuleBean ruleBean) {
        User user = userDao.findOne(ruleBean.getRuleUserId());
        if (ObjectUtils.isEmpty(user)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        if (!ObjectUtils.isEmpty(ruleBean.getRuleId())) {
            List<Rule1> allByRule1RuleId = rule1Dao.findAllByRule1RuleId(ruleBean.getRuleId());
            rule1Dao.delete(allByRule1RuleId);
            List<Rule2> allByRule2RuleId = rule2Dao.findAllByRule2RuleId(ruleBean.getRuleId());
            rule2Dao.delete(allByRule2RuleId);
        }
        RuleBean bean = new RuleBean();
        Rule rule=new Rule();
        BeanUtils.copyProperties(ruleBean,rule);
        Rule save = ruleDao.save(rule);
        BeanUtils.copyProperties(save,bean);
        List<Rule1> rule1s = ruleBean.getRule1s();
        if (ObjectUtils.isEmpty(rule1s)) {
            throw new MyException(ResultEnum.SECTOR_NOTNULL);
        }
        rule1s.forEach(rule1 -> {
            Sector sector = sectorDao.findOne(rule1.getRule1SectorId());
            if (ObjectUtils.isEmpty(sector)) {
                throw new MyException(ResultEnum.SECTOR_ERROR);
            }
            rule1.setRule1RuleId(save.getRuleId());
        });
        List<Rule1> save1 = rule1Dao.save(rule1s);
        bean.setRule1s(save1);
        List<Rule2> rule2s = ruleBean.getRule2s();
        if (ObjectUtils.isEmpty(rule2s)) {
            throw new MyException(ResultEnum.USER_NOTNULL);
        }
        rule2s.forEach(rule2 -> {
            User userDaoOne = userDao.findOne(rule2.getRule2UserId());
            if (ObjectUtils.isEmpty(userDaoOne)) {
                throw new MyException(ResultEnum.USER_ERROR);
            }
            rule2.setRule2RuleId(save.getRuleId());
        });
        List<Rule2> save2 = rule2Dao.save(rule2s);
        bean.setRule2s(save2);
        return bean;
    }


}
